package here.controller;

import java.io.IOException;
import java.util.Hashtable;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletHere extends HttpServlet{

private static final long serialVersionUID = -3268187674094958357L;
	
	private Hashtable<String, AbstractCommand> commands;
	
	public void init(ServletConfig config) throws ServletException {
		
		this.commands = new Hashtable<String, AbstractCommand>();		
		
		this.commands.put("Login", new Login());
		this.commands.put("cadastroUsuario", new CadastroUsuario());
		this.commands.put("CadastroLocal", new CadastroLocal());
		this.commands.put("ListarUsuarios", new ListarUsuarios());
				
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
							throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
							throws ServletException, IOException {
		String cmd = request.getParameter("comando");

		try {
			AbstractCommand command = (AbstractCommand) commands.get(cmd);			
			command.execute(request, response);		
			
		}catch(Exception e){
			request.setAttribute("msgErro", "Erro grave de Sistema: Contacte o Administrador \n"+ e.getMessage());						
			request.getRequestDispatcher("/error.jsp").forward(request, response);	
		}	
	}
}
